#include "activityexecutionview.h"

#include <QtCore/QEvent>
#include <QtGui/QHBoxLayout>
#include <QtGui/QLabel>
#include <QtGui/QPushButton>
#include <QtGui/QScrollArea>
#include <QtGui/QVBoxLayout>

using namespace swp;

ActivityExecutionView::ActivityExecutionView(QWidget* parent) : QFrame(parent),
    m_mainLayout(0),
    m_mapping()
{
    QLabel* header = new QLabel(tr("Activity detail"));
    QFrame* topFrame = new QFrame();

    QVBoxLayout* layout = new QVBoxLayout(this);
    layout->setContentsMargins(0, 0, 0, 0);
    layout->setSpacing(0);

    QScrollArea* scrollArea = new QScrollArea();

    scrollArea->setWidget(topFrame);
    scrollArea->setWidgetResizable(true);

    layout->addWidget(header);
    layout->addWidget(scrollArea);

    m_mainLayout = new QVBoxLayout(topFrame);
    m_mainLayout->setContentsMargins(2, 2, 2, 2);
    m_mainLayout->setSpacing(2);
    m_mainLayout->addStretch();

    topFrame->setLayout(m_mainLayout);

    setLayout(layout);
}

ActivityExecutionView::~ActivityExecutionView()
{

}

void ActivityExecutionView::addActivity(const QString& name,
    const QString& executions,
    const QString& avgExecutions, const QString& cost,
    const QString& costAverage, const QString& duration,
    const QString& durationAverage)
{
    QFrame* activityFrame = new QFrame();

    QFrame* headerFrame = new QFrame();
    headerFrame->installEventFilter(this);
    headerFrame->setObjectName("activityHeader");
    QLabel* nameL = new QLabel(name);
    QLabel* headerButton = new QLabel(" ");
    headerButton->setObjectName("headerButton");

    QHBoxLayout* headerLayout = new QHBoxLayout(headerFrame);
    headerLayout->addWidget(nameL);
    headerLayout->addStretch();
    headerLayout->addWidget(headerButton);

    headerFrame->setLayout(headerLayout);

    QFrame* valuesFrame = new QFrame();
    valuesFrame->hide();

    QLabel* executionsL = new QLabel(tr("Executions:"));
    executionsL->setObjectName("valueLabel");
    QLabel* avgExecutionsL = new QLabel(tr("Average executions (per run):"));
    avgExecutionsL->setObjectName("valueLabel");
    QLabel* costL = new QLabel(tr("Cost:"));
    costL->setObjectName("valueLabel");
    QLabel* avgCostL = new QLabel(tr("Average cost (per run):"));
    avgCostL->setObjectName("valueLabel");
    QLabel* durationL = new QLabel(tr("Duration:"));
    durationL->setObjectName("valueLabel");
    QLabel* avgDurationL = new QLabel(tr("Average duration (per run):"));
    avgDurationL->setObjectName("valueLabel");

    QLabel* executionsV = new QLabel(executions);
    QLabel* avgExecutionsV = new QLabel(avgExecutions);
    QLabel* costV = new QLabel(cost);
    QLabel* avgCostV = new QLabel(costAverage);
    QLabel* durationV = new QLabel(duration);
    QLabel* avgDurationV = new QLabel(durationAverage);

    QGridLayout* valuesLayout = new QGridLayout(valuesFrame);
    valuesLayout->setContentsMargins(20, 10, 20, 20);

    valuesLayout->addWidget(durationL, 0, 0);
    valuesLayout->addWidget(durationV, 0, 1);
    valuesLayout->addWidget(avgDurationL, 1, 0);
    valuesLayout->addWidget(avgDurationV, 1, 1);
    valuesLayout->addItem(new QSpacerItem(20, 15,
        QSizePolicy::Minimum, QSizePolicy::Minimum), 0, 3);
    valuesLayout->addItem(new QSpacerItem(20, 15,
        QSizePolicy::Minimum, QSizePolicy::Minimum), 1, 3);

    valuesLayout->addWidget(costL, 0, 4);
    valuesLayout->addWidget(costV, 0, 5);
    valuesLayout->addWidget(avgCostL, 1, 4);
    valuesLayout->addWidget(avgCostV, 1, 5);
    valuesLayout->addItem(new QSpacerItem(20, 15,
        QSizePolicy::Minimum, QSizePolicy::Minimum), 0, 6);
    valuesLayout->addItem(new QSpacerItem(20, 15,
        QSizePolicy::Minimum, QSizePolicy::Minimum), 1, 6);

    valuesLayout->addWidget(executionsL, 0, 7);
    valuesLayout->addWidget(executionsV, 0, 8);
    valuesLayout->addWidget(avgExecutionsL, 1, 7);
    valuesLayout->addWidget(avgExecutionsV, 1, 8);
    valuesLayout->addItem(new QSpacerItem(0, 0,
        QSizePolicy::Expanding, QSizePolicy::Minimum), 0, 9);
    valuesLayout->addItem(new QSpacerItem(0, 0,
        QSizePolicy::Expanding, QSizePolicy::Minimum), 1, 9);

    //valuesLayout->addItem(new QSpacerItem(0, 0,
        //QSizePolicy::Minimum, QSizePolicy::Expanding), 2, 0);

    valuesFrame->setLayout(valuesLayout);

    QVBoxLayout* activityLayout = new QVBoxLayout(activityFrame);
    activityLayout->setContentsMargins(0, 0, 0, 0);

    activityLayout->addWidget(headerFrame);
    activityLayout->addWidget(valuesFrame);

    activityFrame->setLayout(activityLayout);

    m_mainLayout->insertWidget(m_mainLayout->count() - 1, activityFrame);

    m_mapping[headerFrame] = valuesFrame;
}

bool ActivityExecutionView::eventFilter(QObject* watched, QEvent* event)
{
    bool ret = false;

    QFrame* frame = qobject_cast<QFrame*>(watched);

    if (m_mapping.contains(frame) && event->type() ==
        QEvent::MouseButtonRelease)
    {
        QFrame* values = m_mapping[frame];
        if (values->isHidden())
        {
            values->show();
        }
        else
        {
            values->hide();
        }

        ret = true;
    }

    return ret;
}
